package com.fmyd.qizhuazhua.ui.base;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fmyd.qizhuazhua.R;

/**
 * Created by honghaonan on 2018/4/24.
 */

public abstract class BaseToolBarActivity extends BaseActivity {
    private LinearLayout llRoot;

    @Override
    protected void afterSetContentView() {
        setContentView(R.layout.activity_tool_bar);
        llRoot = (LinearLayout) findViewById(R.id.llRoot);
        Toolbar tb = (Toolbar) findViewById(R.id.tb);
        ViewStub vsLeftImage = (ViewStub) findViewById(R.id.vsLeftImage);
        ViewStub vsTitle = (ViewStub) findViewById(R.id.vsTitle);
        ViewStub vsRightText = (ViewStub) findViewById(R.id.vsRightText);
        ViewStub vsRightImage = (ViewStub) findViewById(R.id.vsRightImage);
        ViewStub vsRightButton = (ViewStub) findViewById(R.id.vsRightButton);

        if (null != llRoot && getContentView() > 0) {
            LayoutInflater.from(this).inflate(getContentView(), llRoot);
        }

        setSupportActionBar(tb);

        //设置Toolbar背景和高度
        if (0 != topBackground()) {
            tb.setBackgroundResource(topBackground());
        }
        if (0 != topHeight()) {
            tb.setMinimumHeight(topHeight());
        }

        //设置标题显示位置及内容
        getSupportActionBar().setDisplayShowTitleEnabled(!titleInMiddle());
        if (null != titleText()) {
            if (titleInMiddle()) {
                TextView tvTitle = (TextView) vsTitle.inflate();
                tvTitle.setText(titleText());
            } else {
                getSupportActionBar().setTitle(titleText());
            }
        }

        //设置左侧按键是否显示及监听事件（一般为返回键）
        getSupportActionBar().setDisplayHomeAsUpEnabled(hasLeft() && (leftIcon() <= 0));
        if (hasLeft()) {
            if (leftIcon() > 0) {
                ImageView ivLeft = (ImageView) vsLeftImage.inflate();
                ivLeft.setImageResource(leftIcon());
                ivLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onLeftClick();
                    }
                });
            } else {
                tb.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onLeftClick();
                    }
                });
            }
        }

        //设置右侧控件(图片、按钮或纯文本)及监听事件
        if (0 != rightImage()) {
            ImageView ivRight = (ImageView) vsRightImage.inflate();
            ivRight.setImageResource(rightImage());
            ivRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightClick();
                }
            });
        } else if (null != rightButton()) {
            Button btnRight = (Button) vsRightButton.inflate();
            btnRight.setText(rightButton());
            btnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightClick();
                }
            });
        } else if (null != rightText()) {
            TextView tvRight = (TextView) vsRightText.inflate();
            tvRight.setText(rightText());
            tvRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightClick();
                }
            });
        }

        if (getLogo() > 0) {
            tb.setLogo(getLogo());
        }

        if (autoHideNavigation()) {
            //监听Menu菜单显示/关闭状态
            getSupportActionBar().addOnMenuVisibilityListener(new ActionBar.OnMenuVisibilityListener() {
                @Override
                public void onMenuVisibilityChanged(boolean isVisible) {
                    //Menu等部分页面元素发生变化时Navigation会自动出现，所以在控制它们消失的同时需要隐藏Navigation
                    hideNavigation();
                }
            });
        }
    }

    @Override
    protected void onResume() {
        if (autoHideNavigation()) {
            hideNavigation();
        }
        super.onResume();
    }

    @Override
    protected void dismissWaiting() {
        super.dismissWaiting();
        if (autoHideNavigation()) {
            hideNavigation();
        }
    }

    /**
     * 标题文字
     */
    protected abstract String titleText();

    /**
     * 标题是否居中，默认居中
     */
    protected boolean titleInMiddle() {
        return true;
    }

    /**
     * Toolbar背景
     */
    protected int topBackground() {
        return 0;
    }

    /**
     * Toolbar高度
     */
    protected int topHeight() {
        return 0;
    }

    /**
     * Toolbar左侧logo，若为0则不显示，默认为0
     */
    protected int getLogo() {
        return 0;
    }

    /**
     * Toolbar是否有左侧控件，默认有
     */
    protected boolean hasLeft() {
        return true;
    }

    /**
     * Toolbar左侧控件图标，默认不设置
     */
    protected int leftIcon() {
        return 0;
    }

    /**
     * Toolbar左侧控件点击事件，默认为返回
     */
    protected void onLeftClick() {
        onBackPressed();
    }

    /**
     * Toolbar右侧TextView文字，若为null则不显示，默认为null
     */
    protected String rightText() {
        return null;
    }

    /**
     * Toolbar右侧ImageView图片，若为0则不显示，默认为0
     */
    protected int rightImage() {
        return 0;
    }

    /**
     * Toolbar右侧Button文字，若为null则不显示，默认为null
     */
    protected String rightButton() {
        return null;
    }

    /**
     * Toolbar右侧控件点击事件
     */
    protected void onRightClick() {

    }

    /**
     * 是否自动隐藏底部导航栏，默认不自动隐藏
     */
    protected boolean autoHideNavigation() {
        return false;
    }

    /**
     * 隐藏底部导航栏
     */
    public void hideNavigation() {
        llRoot.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LOW_PROFILE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
//            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE //会导致actionbar占据主页面的空间
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }
}
